package game.demo.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import game.demo.pojo.ADemo;
import game.demo.dao.IADemoDao;
import game.util.DBUtil;


/**
 * ADemo的Dao实现类
 * @author WangshMac
 */
public class ADemoDaoImpl implements IADemoDao
{
	/**
	 * 保存一条记录(添加)
	 * JDBC四步走
	 */
	@Override
	public int insertDao(ADemo demo)
	{
		/*standardJDBC();*/
		String sql = "INSERT INTO `game`.`a_demo`(`name`, `content`, `status`, `createTime`, `updateTime`, `pubTime`) VALUES "
				+ "(?,?,?,?,?,?)" ; 
		
		DBUtil dbUtil = new DBUtil() ; 
		/* 获取一个链接 */
		Connection connection = dbUtil.getConnection() ; 
		PreparedStatement pst = null ;
		ResultSet rs = null ; 
		try
		{
			/* 创建sql语句 
			 * 参数2:是为了返回主键
			 * */
			pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			/* 为?(占位符)赋值 */
			pst.setObject(1, demo.getName());
			pst.setObject(2, demo.getContent());
			pst.setObject(3, demo.getStatus());
			pst.setObject(4, demo.getCreateTime());
			pst.setObject(5, demo.getUpdateTime());
			pst.setObject(6, demo.getPubTime());
			int res = pst.executeUpdate() ;
			/* 获取一下数据库中的主键 */
			rs = pst.getGeneratedKeys() ;
			/* 主键可是一行一列 
			 * getInt只有在不知道列名的时候,就使用第几列
			 * */
			if(rs.next())
			{
				int id = rs.getInt(1);
				demo.setId(id);
			}
			return res ; 
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			dbUtil.closeAll(connection, pst, rs);
		}
		return 0;
	}

	@Override
	public int updateDao(ADemo demo)
	{
		/*standardJDBC();*/
		String sql = "update a_demo set name = ? , content = ? , status = ? , "
				+ "createTime = ? , updateTime = ? , pubTime = ? "
				+ "where id = ? " ;
		
		DBUtil dbUtil = new DBUtil() ; 
		/* 获取一个链接 */
		Connection connection = dbUtil.getConnection() ; 
		PreparedStatement pst = null ;
		ResultSet rs = null ; 
		try
		{
			/* 创建sql语句 
			 * 参数2:是为了返回主键
			 * */
			pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			/* 为?(占位符)赋值 */
			pst.setObject(1, demo.getName());
			pst.setObject(2, demo.getContent());
			pst.setObject(3, demo.getStatus());
			pst.setObject(4, demo.getCreateTime());
			pst.setObject(5, demo.getUpdateTime());
			pst.setObject(6, demo.getPubTime());
			pst.setObject(7, demo.getId());
			int res = pst.executeUpdate() ;
			/* 获取一下数据库中的主键 */
			rs = pst.getGeneratedKeys() ;
			/* 主键可是一行一列 
			 * getInt只有在不知道列名的时候,就使用第几列
			 * */
			if(rs.next())
			{
				int id = rs.getInt(1);
				demo.setId(id);
			}
			return res ; 
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			dbUtil.closeAll(connection, pst, rs);
		}
		return 0;
	}
	
	public int deleteDao(ADemo demo)
	{
		String sql = "DELETE FROM `game`.`a_demo` WHERE `id` = ?";
		DBUtil db = new DBUtil();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null ;
		try
		{
			ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			/*占位符赋值*/
			ps.setObject(1, demo.getId());
			int res = ps.executeUpdate();
			rs = ps.getGeneratedKeys(); 
			if(rs.next())
			{
				int id = rs.getInt(1);
				demo.setId(id);
			}
			return res;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			db.closeAll(con, ps, rs);
		}
		return 0;
	}

	@Override
	public ADemo findOneDao(Map<String, Object> condMap)
	{
		/*standardJDBC();*/
		String sql = "select * from a_demo where id = ? " ; 
		
		DBUtil dbUtil = new DBUtil() ; 
		/* 获取一个链接 */
		Connection connection = dbUtil.getConnection() ; 
		PreparedStatement pst = null ;
		ResultSet rs = null ; 
		try
		{
			/* 创建sql语句 
			 * 参数2:是为了返回主键
			 * */
			pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			/* 为?(占位符)赋值 */
			pst.setObject(1, condMap.get("id"));
			/* 获取一下数据库中的主键 */
			rs = pst.executeQuery() ;
			/* 主键可是一行一列 
			 * getInt只有在不知道列名的时候,就使用第几列
			 * */
			if(rs.next())
			{
				ADemo demo = convertFromDb(rs);
				return demo ; 
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			dbUtil.closeAll(connection, pst, rs);
		}
		return null;
	}

	@Override
	//public List<ADemo> findOneListDao(Map<String, Object> condMap)
	public List<ADemo> findCondListDao(Map<String, Object> condMap)
	{
		/* 存储的结果 */
		List<ADemo> resultList = new ArrayList<ADemo>();
		/*standardJDBC();*/
		String sql = "select * from a_demo" ; 
		
		DBUtil dbUtil = new DBUtil() ; 
		/* 获取一个链接 */
		Connection connection = dbUtil.getConnection() ; 
		PreparedStatement pst = null ;
		ResultSet rs = null ; 
		try
		{
			/* 创建sql语句 
			 * 参数2:是为了返回主键
			 * */
			pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			/* 获取一下数据库中的主键 */
			rs = pst.executeQuery() ;
			/* 主键可是一行一列 
			 * getInt只有在不知道列名的时候,就使用第几列
			 * */
			while(rs.next())
			{
				ADemo demo = convertFromDb(rs);
				/* 一定要将demo增加到List中 */
				resultList.add(demo);
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			dbUtil.closeAll(connection, pst, rs);
		}
		return resultList;
	}

	/**
	 * 将数据库里面的记录转换为Java对象
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	private ADemo convertFromDb(ResultSet rs) throws SQLException
	{
		/* 将数据库中的一条记录,与java中的对象一一对应起来 */
		int id = rs.getInt("id");
		String name = rs.getString("name");
		String content = rs.getString("content");
		byte status = rs.getByte("status");
		Date createTime = rs.getTimestamp("createTime");
		Date updateTime = rs.getTimestamp("updateTime");
		Date pubTime = rs.getTimestamp("pubTime");
		
		ADemo demo = new ADemo() ; 
		demo.setId(id);
		demo.setName(name);
		demo.setContent(content);
		demo.setStatus(status);
		demo.setCreateTime(createTime);
		demo.setUpdateTime(updateTime);
		demo.setPubTime(pubTime);
		return demo;
	}
}
